package ssm.utils;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TimeUtils {

    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    //计算出前12个月
    public static String[] getLast6Months() {

        String[] last6Months = new String[6];

        Calendar cal = Calendar.getInstance();
        for (int i = 0; i < 6; i++) {
            if (cal.get(Calendar.MONTH) - i + 1 < 1) {
                last6Months[i] = cal.get(Calendar.YEAR) - 1 + "-" + fillZero((cal.get(Calendar.MONTH) - i + 1 + 12 * 1));
            } else {
                last6Months[i] = cal.get(Calendar.YEAR) + "-" + fillZero((cal.get(Calendar.MONTH) - i + 1));
            }

            //System.out.println(last12Months[11-i]);
        }

        return last6Months;
    }

    public static String fillZero(int i) {
        String str = "";
        if (i > 0 && i < 10) {
            str = "0" + i;
        } else {
            str = "" + i;
        }
        return str;

    }


    //2个时间字符串比大小
    public static boolean compare_date_1big2(String date1, String date2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = df.parse(date1);
            Date d2 = df.parse(date2);
            if (d1.getTime() > d2.getTime()) {

                return true;
            } else if (d1.getTime() < d2.getTime()) {

                return false;
            } else {
                return false;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;

    }




    public static String getToday(){
        return sdf.format(new Date());
    }



    public static String [] getLast14Days(String date) throws ParseException {
        String [] last14Days=new String[14];
        for(int i=0;i<14;i++){
            date=caladdDay(date);
            last14Days[i]=date;
        }
        return last14Days;
}

    public static String caladdDay(String oldTime)throws ParseException{

        Date time=sdf.parse(oldTime);
        Calendar cal=Calendar.getInstance();
        cal.setTime(time);
        //前一天
        cal.add(Calendar.DATE, -1);
        String lastTime=sdf.format(cal.getTime());
        return lastTime;
    }


  /*  public static void main(String[] args) {
        try {
            String [] aa=TimeUtils.getLast14Days("2018-03-04");
            System.out.println(Arrays.toString(aa));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }*/



  public static boolean isZhoumo(String date) {
      Date bdate = null;
      try {
          bdate = sdf.parse(date);
      } catch (ParseException e) {
          e.printStackTrace();
      }
      Calendar cal = Calendar.getInstance();
      cal.setTime(bdate);
      if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY)
      {
          return true;
      }
      return false;
  }


    public static List<String> getListDate(String startDate,String endDate) {
      try {
          Date d1 = sdf.parse(startDate);//定义起始日期
            Date d2 = sdf.parse(endDate);//定义结束日期
            Calendar dd = Calendar.getInstance();//定义日期实例
            dd.setTime(d1);//设置日期起始时间
            ArrayList<String> dates = new ArrayList<String>();
            while (dd.getTime().getTime()<d2.getTime()||dd.getTime().getTime()==d2.getTime()) {
                String str = sdf.format(dd.getTime());
                dates.add(str);
                dd.add(Calendar.DAY_OF_MONTH, 1);//进行当前日期月份加1
            }
            return dates;
      }catch (Exception e){
          System.out.println("出现异常了");
          return null;
      }

    }

//   public static void main(String[] args) {
//       System.out.println(isZhoumo("20108-04-07"));
//    }



    public static List<String> findDates(String start, String end) throws ParseException {
        SimpleDateFormat sdff=new SimpleDateFormat("yyyy-MM-dd");
        Date dBegin = sdff.parse(start);
        Date dEnd = sdff.parse(end);
        List lDate = new ArrayList();
        lDate.add(start);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime()))
        {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(sdff.format(calBegin.getTime()));
        }
        return lDate;
    }

}


